<template>
	<view class="content">
		<u-notice-bar v-if="flight_inquiry_off == 0" :is-circular='false' duration='2000'
			:list="['温馨提示：如有中转请选择无航班号','航班号查询-接机：系统会默认时间会延后30分钟','航班号查询-送机：系统会默认会提前4小时']"></u-notice-bar>
		<u-notice-bar v-if="flight_inquiry_off == 1" :is-circular='false' :list="['温馨提示：送机请提前3-5小时']"></u-notice-bar>

		<view class="content_box">
			<template v-if="flight_inquiry_off == 0">

				<u-form labelWidth='23' labelPosition="left" :model="flight_inquiry_data">
					<u-form-item borderBottom leftIcon="level" :leftIconStyle="{'color': '#fa3534'}">
						<u-input v-model="flight_inquiry_data.num" :border="false" disabledColor="#ffffff"
							placeholder="请输入航班号, 如 EU8887"></u-input>
					</u-form-item>
					<u-form-item borderBottom leftIcon="calendar-fill" :leftIconStyle="{'color': '#ff9900'}">
						<u-input v-model="flight_inquiry_data.date" :border="false" @click="data_btn"
							disabledColor="#ffffff" :placeholder="'请选择航班日期'  "></u-input>
					</u-form-item>
				</u-form>
				<view
					style="margin-top: 40rpx;margin-bottom: 30rpx;display: flex;padding: 0 20rpx;justify-content: space-evenly;">
					<view style="width: 250rpx;"><u-button type="success" @click="noflightnumber">无航班号</u-button></view>
					<view style="width: 250rpx;"><u-button type="primary" @click="queryNow_btn">立即查询</u-button>
					</view>
				</view>

			</template>
			<template v-if="flight_inquiry_off == 1">

				<u-form labelWidth='23' labelPosition="left" :model="flight_inquiry_data">
					<u-form-item borderBottom leftIcon="clock-fill" :leftIconStyle="{'color': '#2196f3'}">

						<view style="display: flex;">
							<u-input style="width: 190rpx;" @click="time_btn" v-model="noflight_inquiry_data.date"
								:border="false" disabled disabledColor="#ffffff" :placeholder="'请选择日期'  "></u-input>
							:
							<u-input maxlength='4' style="width: 420rpx;" type='number'
								v-model="noflight_inquiry_data.tiem" :border="false" disabledColor="#ffffff"
								:placeholder="'请输入时间,格式：1201为12:01'  "></u-input>
						</view>
					</u-form-item>
				</u-form>
				<view style="color: red;margin-left: 27px;font-size: 25rpx;">{{prompt}}</view>
				<view
					style="margin-top: 40rpx;margin-bottom: 30rpx;display: flex;padding: 0 20rpx;justify-content: space-evenly;">
					<view style="width: 250rpx;"><u-button @click="thereflightnumber" type="success">有航班号</u-button>
					</view>
					<view style="width: 250rpx;"><u-button @click="ok_btn" type="primary">确认</u-button></view>
				</view>


			</template>

		</view>
		<view class="flight-list" v-if="queryNow_list.length > 0 && flight_inquiry_off == 0">

			<view class="no-service" v-if="isService">温馨提示：暂时只支持“成都双流机场和天府机场”业务</view>

			<view @click="service_list_btn(item)" class="list-item" v-for="(item,index) in queryNow_list" :key="index">
				<view class="li-main">
					<view class="point-text">
						<view class="time">{{item.depScheduled.split(' ')[1]}}</view>
						<view class="port">{{item.depPort}}{{item.depTerminal}}</view>
					</view>

					<u-icon name="/static/icons/arrow-right.png" width="70" height="24"></u-icon>

					<view class="point-text">
						<!-- o.arrScheduled.split(' ')[1] -->
						<view class="time">{{item.arrScheduled.split(' ')[1]}}</view>
						<view class="port">{{item.arrPort}}{{item.arrTerminal}}</view>
					</view>
				</view>

				<view class="bottom-info">
					<view class="flight-num">
						{{item.flightNo}}
						<label style="color:#ff7300 ">（{{flightNo_fun(item)}}）</label>

					</view>
					<view class="flight-date">{{item.arrScheduled.split(' ')[0]}}</view>
				</view>
			</view>
		</view>
		<!-- 弹窗 -->

		<template>
			<!-- 日期 -->
			<u-calendar @close="calendar_close" @change="calendar_confirm" :max-date="'2040-12-30'" :monthNum="12"
				color="#41A863" v-model="calendar_show"></u-calendar>
			<!-- 时间 -->
			<u-picker :params="params" @confirm="time_confirm" @cancel="time_cancel" v-model="time_show"
				:default-time="time_value" mode="time"></u-picker>
		</template>
	</view>
</template>

<script>
	export default {

		data() {
			return {
				params: {
					year: true,
					month: true,
					day: true,
					// hour: true,
					// minute: true,
					// second: true
				},
				isService: false,
				queryNow_list: [],
				flight_inquiry_off: 0,
				flight_inquiry_data: {
					num: "",
					date: "",
					travelWay: '1'
				},
				noflight_inquiry_data: {
					date: '',
					tiem: '',
				},
				calendar_show: false,
				time_value: '',
				time_show: false,
				prompt: '',
			}
		},
		onReady() {
			let currentDate = new Date();

			let year = currentDate.getFullYear();
			let month = String(currentDate.getMonth() + 1).padStart(2, '0'); // 月份从0开始，需要加1，然后补0
			let day = String(currentDate.getDate()).padStart(2, '0');
			let hours = String(currentDate.getHours()).padStart(2, '0');
			let minutes = String(currentDate.getMinutes()).padStart(2, '0');
			let seconds = String(currentDate.getSeconds()).padStart(2, '0');

			// let formattedDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
			let formattedDate = `${year}-${month}-${day}`;
			this.time_value = formattedDate


		},
		onLoad(e) {

			if (e.info) {
				this.flight_inquiry_data.num = e.info
			}
			if (e.date) {
				this.flight_inquiry_data.date = e.date
				this.queryNow_btn()
			}


		},
		methods: {
			// 判断是接机还是送接
			flightNo_fun(e) {
				console.log(e)
				let data = ''
				if (e.depPort.includes('天府') || e.depPort.includes('双流')) {
					data = '送机'
				} else if (e.arrPort.includes('天府') || e.arrPort.includes('双流')) {
					data = '接机'
				} else {
					data = '机场暂不支持'
				}
				return data
			},
			noflightnumber() {
				this.flight_inquiry_off = 1
				this.flight_inquiry_data.date = ''
				this.flight_inquiry_data.num = ''
				this.queryNow_list = []
			},
			thereflightnumber() {
				this.flight_inquiry_off = 0
				this.noflight_inquiry_data.date = ''
				this.noflight_inquiry_data.tiem = ''
			},
			data_btn() {
				this.calendar_show = true
			},
			calendar_close() {
				this.calendar_show = false
			},
			calendar_confirm(e) {
				console.log(e)
				if (this.flight_inquiry_off == 0) {
					this.flight_inquiry_data.date = e.result
				} else {
					this.noflight_inquiry_data.date = e.result
				}
				this.calendar_show = false
			},
			calendar_close() {
				this.calendar_show = false
			},
			queryNow_btn() {
				let that = this
				if (!that.flight_inquiry_data.num) {
					return uni.$u.toast('请输入航班号');
				} else if (!that.flight_inquiry_data.date) {
					return uni.$u.toast('请选择日期');
				}
				let ps = that.flight_inquiry_data
				that.$u.api.findTravelWayInfo(ps).then(res => {
					
					if(res.code == 0){
						return uni.$u.toast(res.msg);
					}else{
						that.queryNow_list = res.data.data.list
						
						for(let i =0;i<that.queryNow_list.length;i++){
							if(that.queryNow_list[i].arrPort.includes('天府')){
								that.queryNow_list[i].arrPort = '成都天府机场'
							}
							if(that.queryNow_list[i].arrPort.includes('双流')){
								that.queryNow_list[i].arrPort = '成都双流机场'
							}
							if(that.queryNow_list[i].depPort.includes('天府')){
								that.queryNow_list[i].depPort = '成都天府机场'
							}
							if(that.queryNow_list[i].depPort.includes('双流')){
								that.queryNow_list[i].depPort = '成都双流机场'
							}
							if(that.queryNow_list[i].arrTerminal){
								that.queryNow_list[i].arrTerminal = 'T2'
							}
							if(that.queryNow_list[i].depTerminal){
								that.queryNow_list[i].depTerminal = 'T2'
							}
							if(that.queryNow_list[i].arrTerminal.includes('1')){
								that.queryNow_list[i].arrTerminal = 'T1'
							}
							if(that.queryNow_list[i].arrTerminal.includes('2')){
								that.queryNow_list[i].arrTerminal = 'T2'
							}
							if(that.queryNow_list[i].depTerminal.includes('1')){
								that.queryNow_list[i].depTerminal = 'T1'
							}
							if(that.queryNow_list[i].depTerminal.includes('2')){
								that.queryNow_list[i].depTerminal = 'T2'
							}
						}
						that.isService = true
					}

				})
			},

			time_btn() {
				this.time_show = true
			},
			time_cancel() {
				this.time_show = false
			},
			time_confirm(e) {
				console.log(e)

				// this.noflight_inquiry_data.date = e.year + "-" + e.month + "-" + e.day + " " + e.hour + ":" + e.minute;
				// this.noflight_inquiry_data.tiem = new Date(`${this.noflight_inquiry_data.date}:00`).getTime() // 将毫秒转换为秒
				this.noflight_inquiry_data.date = e.year + "-" + e.month + "-" + e.day
				console.log(this.noflight_inquiry_data.tiem)
				this.time_show = false
			},

			ok_btn() {
				let newtime = Date.now()
				console.log(newtime)

				if (this.noflight_inquiry_data.date == '') {
					return this.prompt = '请选择日期'
				} else {
					this.prompt = ''
				}
				if (this.noflight_inquiry_data.tiem == '') {
					return this.prompt = '请选择时间'
				} else {
					this.prompt = ''
				}
				if (this.noflight_inquiry_data.tiem.length != 4) {
					return this.prompt = '时间格式不对，列12:01请输入1201'
				} else if (this.noflight_inquiry_data.tiem[0] > 2) {
					return this.prompt = '时间格式不对，列12:01请输入1201'
				} else if (this.noflight_inquiry_data.tiem[0] == 2) {
					if (this.noflight_inquiry_data.tiem[1] > 4) {
						return this.prompt = '时间格式不对，列12:01请输入1201'
					} else {
						this.prompt = ''
					}

				} else {
					this.prompt = ''
				}
				let data_time = new Date(
					`${this.noflight_inquiry_data.date+' '+this.noflight_inquiry_data.tiem[0]+this.noflight_inquiry_data.tiem[1]+':'+this.noflight_inquiry_data.tiem[2]+this.noflight_inquiry_data.tiem[3]}:00`
				).getTime() // 将毫秒转换为秒
				if (newtime >= data_time) {
					return this.prompt = '不能小于当前时间'
				} else {
					this.prompt = ''

				}
				this.noflight_inquiry_data.date = this.noflight_inquiry_data.date + ' ' + this.noflight_inquiry_data.tiem[
						0] + this.noflight_inquiry_data.tiem[1] + ':' + this.noflight_inquiry_data.tiem[2] + this
					.noflight_inquiry_data.tiem[3]
				let tabs_page = {
					tabs1: 3, //跳转到添加订单
				}
				uni.setStorageSync('tabs_page', tabs_page);

				let ps = {
					num: '飞机',
					timedate: this.noflight_inquiry_data.date,
					arrPort: '',
					depPort: '',
					flightNo: '',
					depScheduled: '',
					arrScheduled: '',
					depTerminal: '',
					arrTerminal: '',
				}
				uni.setStorageSync('aircraft', ps);
				uni.reLaunch({
					url: '/pages/tabbar/order-list/order-list'
				});

			},
			service_list_btn(e) {
				let that = this
				if (e.depPort.includes('天府') || e.depPort.includes('双流')) {
					let tabs_page = {
						tabs1: 3, //跳转到添加订单
					}
					uni.setStorageSync('tabs_page', tabs_page);
					let data = {
						num: '飞机',
						timedate: '',
						arrPort: e.arrPort,
						depPort: e.depPort,
						flightNo: e.flightNo,
						depScheduled: e.depScheduled,
						arrScheduled: e.arrScheduled,
						depTerminal: e.depTerminal,
						arrTerminal: e.arrTerminal,
					}
					if(e.depPort.includes('双流')){
						data.timedate = that.add3hours(e.depScheduled)
					}else{
						data.timedate = that.add4hours(e.depScheduled)
					}
					
					uni.setStorageSync('aircraft', data);
					uni.reLaunch({
						url: '/pages/tabbar/order-list/order-list'
					});

				} else if (e.arrPort.includes('天府') || e.arrPort.includes('双流')) {
					let tabs_page = {
						tabs1: 3, //跳转到添加订单
					}
					uni.setStorageSync('tabs_page', tabs_page);
					let data = {
						num: '飞机',
						timedate: '',
						arrPort: e.arrPort,
						depPort: e.depPort,
						flightNo: e.flightNo,
						depScheduled: e.depScheduled,
						arrScheduled: e.arrScheduled,
						depTerminal: e.depTerminal,
						arrTerminal: e.arrTerminal,
					}
					data.timedate = that.add30Minutes(e.arrScheduled)
					uni.setStorageSync('aircraft', data);
					uni.reLaunch({
						url: '/pages/tabbar/order-list/order-list'
					});
				} else {
					uni.$u.toast('很抱歉，暂时只支持“成都天府和双流机场”业务');
				}

			},
			add30Minutes(dateTimeStr) {
				let date = new Date(dateTimeStr);
				date.setMinutes(date.getMinutes() + 30);

				let year = date.getFullYear();
				let month = (date.getMonth() + 1).toString().padStart(2, '0');
				let day = date.getDate().toString().padStart(2, '0');
				let hours = date.getHours().toString().padStart(2, '0');
				let minutes = date.getMinutes().toString().padStart(2, '0');

				let newDateTimeStr = `${year}-${month}-${day} ${hours}:${minutes}`;
				return newDateTimeStr;
			},

			add4hours(dateString) {
				console.log(dateString);
				// 将时间字符串转换为日期对象
				let dateObj = new Date(dateString);

				// 将时间提前4个小时
				dateObj.setHours(dateObj.getHours() - 4);

				// 格式化新的日期时间
				let year = dateObj.getFullYear();
				let month = ("0" + (dateObj.getMonth() + 1)).slice(-2);
				let day = ("0" + dateObj.getDate()).slice(-2);
				let hours = ("0" + dateObj.getHours()).slice(-2);
				let minutes = ("0" + dateObj.getMinutes()).slice(-2);

				let newDateString = year + "-" + month + "-" + day + " " + hours + ":" + minutes;

				console.log(newDateString);
				// 输出新的日期时间字符串
				return newDateString;



			},
			add3hours(dateString) {
				console.log(dateString);
				// 将时间字符串转换为日期对象
				let dateObj = new Date(dateString);
			
				// 将时间提前4个小时
				dateObj.setHours(dateObj.getHours() - 3);
			
				// 格式化新的日期时间
				let year = dateObj.getFullYear();
				let month = ("0" + (dateObj.getMonth() + 1)).slice(-2);
				let day = ("0" + dateObj.getDate()).slice(-2);
				let hours = ("0" + dateObj.getHours()).slice(-2);
				let minutes = ("0" + dateObj.getMinutes()).slice(-2);
			
				let newDateString = year + "-" + month + "-" + day + " " + hours + ":" + minutes;
			
				console.log(newDateString);
				// 输出新的日期时间字符串
				return newDateString;
			
			
			
			},
			add2hours(dateString) {
				console.log(dateString);
				// 将时间字符串转换为日期对象
				let dateObj = new Date(dateString);

				// 将时间提前4个小时
				dateObj.setHours(dateObj.getHours() - 2);

				// 格式化新的日期时间
				let year = dateObj.getFullYear();
				let month = ("0" + (dateObj.getMonth() + 1)).slice(-2);
				let day = ("0" + dateObj.getDate()).slice(-2);
				let hours = ("0" + dateObj.getHours()).slice(-2);
				let minutes = ("0" + dateObj.getMinutes()).slice(-2);

				let newDateString = year + "-" + month + "-" + day + " " + hours + ":" + minutes;

				console.log(newDateString);
				// 输出新的日期时间字符串
				return newDateString;



			},





		},

	}
</script>

<style lang="scss">
	.content_box {
		width: 90%;
		margin: auto;
		background-color: #fff;
		border-radius: 10rpx;
		padding: 10rpx 20rpx;
	}

	.u-form-item {
		padding: 10rpx 0 !important;
	}

	.flight-list {
		background: #73bee257;
		margin: 30rpx 20rpx;
		border-radius: 10rpx;
		padding: 10rpx;

		.no-service {
			border-bottom: 2rpx solid #efefef;
			padding: 10rpx;
			color: #ff9900;
			font-size: 28rpx;
		}

		.list-item {
			&:not(:last-child) {
				border-bottom: 2rpx solid #f3f3f3;
			}

			.li-main {
				display: flex;
				justify-content: space-between;
				padding: 30rpx 10rpx;

				.point-text {

					.time {
						font-size: 30rpx;
						font-weight: 600;
					}

					.port {
						font-size: 30rpx;
					}
				}
			}

			.bottom-info {
				display: flex;
				justify-content: space-between;
				margin-bottom: 10rpx;

				.flight-num {
					font-size: 30rpx;
					font-weight: 600;
					color: #666;
				}

				.flight-date {
					font-size: 30rpx;
					font-weight: 600;
					color: #666;
				}
			}

		}
	}
</style>